package ipad.asia.controller;

import ipad.asia.config.OrderVO;
import ipad.asia.entity.Order;
import ipad.asia.entity.Product;
import ipad.asia.service.consumer.ConsumerFeign;
import ipad.asia.service.order.OrderFeign;
import ipad.asia.service.product.ProductFeign;
import ipad.asia.service.seckill.SecKillFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Controller
public class OrderController {
    @Autowired
    private OrderFeign orderFeign;
    @Autowired
    private ProductFeign productFeign;
    @Autowired
    private ConsumerFeign consumerFeign;
    @Autowired
    private SecKillFeign secKillFeign;

    /**
     *
     * @param model
     * @return
     */
    @RequestMapping("/order/all")
    public String All(HttpServletRequest request,Model model,
                      @RequestParam(value = "sendMessage",required = false)String sendMessage){
        HttpSession session=request.getSession();
        int consumerId=consumerFeign.getName((String)(session.getAttribute("user"))).getId();
        System.out.println(session.getAttribute("user"));
        List<Order> orders = orderFeign.findByConsumerId(consumerId);
        List<Product> products = productFeign.all();
        List<OrderVO> orderVOS=new ArrayList<>();
        for(int i=0;i<orders.size();i++){
            for (int j=0;j<products.size();j++){
                if (orders.get(i).getProductId()==products.get(j).getId()){
                    OrderVO orderVO=new OrderVO();
                    orderVO.setOrder(orders.get(i));
                    orderVO.setProduct(products.get(j));
                    orderVOS.add(orderVO);
                }
            }
        }
        System.out.println(orderVOS);
        model.addAttribute("orderVO",orderVOS);
        return "order/all";
    }

    /**
     *
     * @param model
     * @return
     */
    @RequestMapping("/order/noPay")
    public String noPay(HttpServletRequest request,Model model){
        HttpSession session=request.getSession();
        int consumerId=consumerFeign.getName((String)(session.getAttribute("user"))).getId();
        List<Order> orders = orderFeign.findByStatuesAndConsumerId(0,consumerId);
        List<Product> products = productFeign.all();
        List<OrderVO> orderVOS=new ArrayList<>();
        for(int i=0;i<orders.size();i++){
            for (int j=0;j<products.size();j++){
                if (orders.get(i).getProductId()==products.get(j).getId()){
                    OrderVO orderVO=new OrderVO();
                    orderVO.setOrder(orders.get(i));
                    orderVO.setProduct(products.get(j));
                    orderVOS.add(orderVO);
                }
            }
        }
        System.out.println(orderVOS);
        model.addAttribute("orderVO",orderVOS);
        return "order/noPay";
    }

    /**
     *
     * @param model
     * @return
     */
    @RequestMapping("/order/noSend")
    public String noSend(HttpServletRequest request,Model model){
        HttpSession session=request.getSession();
        int consumerId=consumerFeign.getName((String)(session.getAttribute("user"))).getId();
        List<Order> orders = orderFeign.findByStatuesAndConsumerId(1,consumerId);
        List<Product> products = productFeign.all();
        List<OrderVO> orderVOS=new ArrayList<>();
        for(int i=0;i<orders.size();i++){
            for (int j=0;j<products.size();j++){
                if (orders.get(i).getProductId()==products.get(j).getId()){
                    OrderVO orderVO=new OrderVO();
                    orderVO.setOrder(orders.get(i));
                    orderVO.setProduct(products.get(j));
                    orderVOS.add(orderVO);
                }
            }
        }
        System.out.println(orderVOS);
        model.addAttribute("orderVO",orderVOS);
        return "order/noSend";
    }

    /**
     *
     * @param model
     * @return
     */
    @RequestMapping("/order/haveSend")
    public String haveSend(HttpServletRequest request,Model model){
        HttpSession session=request.getSession();
        int consumerId=consumerFeign.getName((String)(session.getAttribute("user"))).getId();
        List<Order> orders = orderFeign.findByStatuesAndConsumerId(2,consumerId);
        List<Product> products = productFeign.all();
        List<OrderVO> orderVOS=new ArrayList<>();
        for(int i=0;i<orders.size();i++){
            for (int j=0;j<products.size();j++){
                if (orders.get(i).getProductId()==products.get(j).getId()){
                    OrderVO orderVO=new OrderVO();
                    orderVO.setOrder(orders.get(i));
                    orderVO.setProduct(products.get(j));
                    orderVOS.add(orderVO);
                }
            }
        }
        System.out.println(orderVOS);
        model.addAttribute("orderVO",orderVOS);
        return "order/haveSend";
    }

    /**
     *
     * @param model
     * @return
     */
    @RequestMapping("/order/haveReceive")
    public String haveReceive(HttpServletRequest request,Model model){
        HttpSession session=request.getSession();
        int consumerId=consumerFeign.getName((String)(session.getAttribute("user"))).getId();
        List<Order> orders = orderFeign.findByStatuesAndConsumerId(3,consumerId);
        List<Product> products = productFeign.all();
        List<OrderVO> orderVOS=new ArrayList<>();
        for(int i=0;i<orders.size();i++){
            for (int j=0;j<products.size();j++){
                if (orders.get(i).getProductId()==products.get(j).getId()){
                    OrderVO orderVO=new OrderVO();
                    orderVO.setOrder(orders.get(i));
                    orderVO.setProduct(products.get(j));
                    orderVOS.add(orderVO);
                }
            }
        }
        System.out.println(orderVOS);
        model.addAttribute("orderVO",orderVOS);
        return "order/haveReceive";
    }

    /**
     *
     * @param model
     * @return
     */
    @RequestMapping("/order/finish")
    public String finish(@RequestParam(value = "message",required = false)String massage,
            HttpServletRequest request,Model model){
        HttpSession session=request.getSession();
        int consumerId=consumerFeign.getName((String)(session.getAttribute("user"))).getId();
        List<Order> orders = orderFeign.findByStatuesAndConsumerId(4,consumerId);
        List<Product> products = productFeign.all();
        List<OrderVO> orderVOS=new ArrayList<>();
        for(int i=0;i<orders.size();i++){
            for (int j=0;j<products.size();j++){
                if (orders.get(i).getProductId()==products.get(j).getId()){
                    OrderVO orderVO=new OrderVO();
                    orderVO.setOrder(orders.get(i));
                    orderVO.setProduct(products.get(j));
                    orderVOS.add(orderVO);
                }
            }
        }
        System.out.println(orderVOS);
        model.addAttribute("orderVO",orderVOS);
        return "order/finish";
    }

    /**
     *
     * @param model
     * @return
     */
    @RequestMapping("/order/refund")
    public String refund(HttpServletRequest request,Model model){
        HttpSession session=request.getSession();
        int consumerId=consumerFeign.getName((String)(session.getAttribute("user"))).getId();
        List<Order> orders = orderFeign.findByStatuesAndConsumerId(5,consumerId);
        List<Product> products = productFeign.all();
        List<OrderVO> orderVOS=new ArrayList<>();
        for(int i=0;i<orders.size();i++){
            for (int j=0;j<products.size();j++){
                if (orders.get(i).getProductId()==products.get(j).getId()){
                    OrderVO orderVO=new OrderVO();
                    orderVO.setOrder(orders.get(i));
                    orderVO.setProduct(products.get(j));
                    orderVOS.add(orderVO);
                }
            }
        }
        System.out.println(orderVOS);
        model.addAttribute("orderVO",orderVOS);
        return "/order/refund";
    }

    @RequestMapping("/order/setStatue")
    public String setStatue(@RequestParam(value = "id",required = false) int id,
                            @RequestParam(value = "statue",required = false)int statue){
        orderFeign.updateStatue(id, statue);
        return "order/all";
    }

    @RequestMapping("/order/del")
    public String orderDel(@RequestParam(value = "id",required = false)int id,
                           @RequestParam(value = "statue",required = false)int statue,
                           HttpSession session){
        if(statue==0){
            Product product = productFeign.getById(orderFeign.findId(id).getProductId());
            if(product.getHaveDiscount()==1){
                String user= (String) session.getAttribute("user");
                secKillFeign.deleteNoPay(product.getId(),consumerFeign.getName(user).getId());
                orderFeign.orderDel(id);
            }else {
            productFeign.updateNumber(product.getNumber()+1,product.getId());
            orderFeign.orderDel(id);}
        }if (statue==4){
            orderFeign.orderDel(id);
        }
        return "order/all";
    }


    @RequestMapping("/order/confirm")
    public String finish(@RequestParam(value = "orderId",required = false)String orderId,
                         @RequestParam(value = "statue",required = false)String statue,
                         Model model){
        System.out.println("orderId======"+orderId);
        if(orderId==null){
            model.addAttribute("message","你的订单不存在，请刷新后再试！");
            return "order/all";
        }
        orderFeign.updateStatue(Integer.parseInt(orderId), Integer.parseInt(statue));
        model.addAttribute("message","确认收货成功！");
        return "order/all";
    }


}
